package br.com.foxline.atlasmobile.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import br.com.foxline.atlasmobile.R;
import br.com.foxline.atlasmobile.dao.AnnexDao;
import br.com.foxline.atlasmobile.dao.ImovelDao;
import br.com.foxline.atlasmobile.entities.Imovel;
import br.com.foxline.atlasmobile.enums.SituacaoImovel;
import br.com.foxline.atlasmobile.util.UtilConnection;

public class ListCheckAdapter extends ArrayAdapter<Imovel> {

	private final Context context;
	private List<Imovel> list;
	private List<Imovel> listChecked;
	private ImovelDao imovelDAO;
	private AnnexDao annexDao;
	private TextView textTitle;
	private CheckBox chk;

	public ListCheckAdapter(Context context, Boolean... booleans) {
		super(context, R.layout.item_list_check);

		imovelDAO = new ImovelDao(context);
		annexDao = new AnnexDao(context);

		SharedPreferences prefs = context.getSharedPreferences(
				UtilConnection.APP_SHARED_PREFS, 0);

		this.list = imovelDAO.export();

		this.context = context;
		this.listChecked = new ArrayList<Imovel>();
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		this.list = imovelDAO.export();
		this.listChecked.clear();

	}

	@Override
	public Imovel getItem(int position) {
		return list.get(position);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	public List<Imovel> getListChecked() {
		return listChecked;
	}

	public List<Imovel> getFirstListChecked(int n) {
		this.listChecked.clear();

		for (int i = 0; i < (list.size() >= n ? n : list.size()); i++) {
			Imovel imovel = list.get(i);

			if (!listChecked.contains(imovel)) {
				listChecked.add(imovel);
			}
		}
		return listChecked;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View rowView = inflater
				.inflate(R.layout.item_list_check, parent, false);

		Imovel imovel = list.get(position);

		chk = (CheckBox) rowView.findViewById(R.id.checkBox_list);
		chk.setTag(imovel);

		chk.setOnClickListener(checkListener);

		TextView inscri = (TextView) rowView.findViewById(R.id.show_inscricao);
		TextView endereco = (TextView) rowView
				.findViewById(R.id.show_endereco_imovel);
		TextView grupo = (TextView) rowView.findViewById(R.id.show_grupo);
		TextView situacao = (TextView) rowView.findViewById(R.id.show_situacao);

		String string = "#" + imovel.getId() + " - "
				+ imovel.getInscricaoCadastralD() + "."
				+ imovel.getInscricaoCadastralS() + "."
				+ imovel.getInscricaoCadastralQuadra() + "."
				+ imovel.getInscricaoCadastralLote() + "."
				+ imovel.getInscricaoCadastralUnid();

		inscri.setText(string.replace("null", "0"));
		SimpleDateFormat formar = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		endereco.setText(imovel.toString());
		grupo.setText(imovel.getGroupCreate());
		situacao.setText((imovel.getSituacaoImovel() == null ? "" : imovel
				.getSituacaoImovel().toString())
				+ (imovel.getEnviado() != null && imovel.getEnviado() ? (imovel
						.getDateenviado() != null ? " - Enviado em "
						+ formar.format(imovel.getDateenviado()) : "") : ""));

		if (imovel.getSituacaoImovel() == SituacaoImovel.CONCLUIDO)
			situacao.setTextColor(Color.rgb(20, 100, 20));
		else if (imovel.getSituacaoImovel() == SituacaoImovel.CANCELADO)
			situacao.setTextColor(Color.YELLOW);
		else if (imovel.getSituacaoImovel() == SituacaoImovel.PENDENTE_DOC
				|| imovel.getSituacaoImovel() == SituacaoImovel.PENDENTE_PROPRIETARIO
				|| imovel.getSituacaoImovel() == SituacaoImovel.PENDENTE_RECUSADO)
			situacao.setTextColor(Color.RED);

		return rowView;
	}

	private OnClickListener checkListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {

			CheckBox chk = (CheckBox) v;
			Imovel que = (Imovel) chk.getTag();

			if (chk.isChecked() && listChecked.size() <= 20) {
				listChecked.add(que);
			} else if (!chk.isChecked()) {
				listChecked.remove(que);
			} else {
				Toast.makeText(
						context,
						"Limite máximo de transferência excedido (Até 20 imóveis)",
						Toast.LENGTH_SHORT).show();
			}
		}
	};
}
